"""资源管理相关路由"""

from fastapi import APIRouter, Depends, Query
from typing import Optional
from app.models.resources import ResourceList, ResourceQuery
from app.routers.auth import get_current_user
from app.services.cloud_providers import get_cloud_provider

router = APIRouter()

@router.get("/resources", response_model=ResourceList)
async def get_resources(
    region: Optional[str] = Query(None, description="区域筛选"),
    current_user: dict = Depends(get_current_user)
):
    """获取云资源列表"""
    
    # 获取云服务提供商
    provider = get_cloud_provider(
        current_user["provider"],
        current_user["access_key"],
        current_user["secret_key"]
    )
    
    # 获取各类资源
    ecs_instances = provider.get_ecs_instances(region)
    oss_buckets = provider.get_oss_buckets(region)
    rds_instances = provider.get_rds_instances(region)
    slb_instances = provider.get_slb_instances(region)
    
    return ResourceList(
        ecs=ecs_instances,
        oss=oss_buckets,
        rds=rds_instances,
        slb=slb_instances
    )

@router.get("/regions")
async def get_regions(current_user: dict = Depends(get_current_user)):
    """获取可用区域列表"""
    
    # 根据云厂商返回区域列表
    regions = {
        "aliyun": [
            {"id": "cn-hangzhou", "name": "华东1(杭州)"},
            {"id": "cn-shanghai", "name": "华东2(上海)"},
            {"id": "cn-beijing", "name": "华北2(北京)"},
            {"id": "cn-shenzhen", "name": "华南1(深圳)"},
            {"id": "cn-qingdao", "name": "华北1(青岛)"},
            {"id": "cn-zhangjiakou", "name": "华北3(张家口)"},
            {"id": "cn-huhehaote", "name": "华北5(呼和浩特)"},
            {"id": "cn-wulanchabu", "name": "华北6(乌兰察布)"},
            {"id": "cn-chengdu", "name": "西南1(成都)"},
            {"id": "cn-heyuan", "name": "华南2(河源)"},
            {"id": "cn-guangzhou", "name": "华南3(广州)"},
            {"id": "cn-wuhan-lr", "name": "华中1(武汉)"},
            {"id": "cn-xian", "name": "西北1(西安)"}
        ],
        "tencent": [
            {"id": "ap-beijing", "name": "北京"},
            {"id": "ap-shanghai", "name": "上海"},
            {"id": "ap-guangzhou", "name": "广州"},
            {"id": "ap-nanjing", "name": "南京"},
            {"id": "ap-chengdu", "name": "成都"},
            {"id": "ap-chongqing", "name": "重庆"},
            {"id": "ap-hongkong", "name": "中国香港"},
            {"id": "ap-singapore", "name": "新加坡"}
        ],
        "huawei": [
            {"id": "cn-north-1", "name": "华北-北京一"},
            {"id": "cn-north-2", "name": "华北-北京四"},
            {"id": "cn-east-2", "name": "华东-上海二"},
            {"id": "cn-east-3", "name": "华东-上海一"},
            {"id": "cn-south-1", "name": "华南-广州"},
            {"id": "cn-southwest-2", "name": "西南-贵阳一"}
        ]
    }
    
    user_regions = regions.get(current_user["provider"], [])
    return {"regions": user_regions}
